package br.com.Dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import br.com.Conexao.conexao;
import br.com.Modelo.Endereco;
import br.com.Modelo.UsuarioCliente;
import br.com.Modelo.UsuarioComerciante;

public class UsuarioComercianteDao {
	
	private Connection connection;
	
	public UsuarioComercianteDao(){
		
		this.connection = new conexao().getConnection();
		
	}
	
	 public void adiciona(UsuarioComerciante usuario) {
	    	
	    	if(isLoginEqual(usuario)==false){
	    		
		    String sql = "insert into tb_usuario_comerciante " +
		            "(nome,login,senha,ramo,cnpj,autenticacao,idPagina,qtd_cupons,porcentagem_cupom)" +
		            " values (?,?,?,?,?,?,?,?,?)";
		    System.out.print("entrou aqui");
		    try {
		        // prepared statement para inserção
		        PreparedStatement stmt = (PreparedStatement) connection.prepareStatement(sql);
               System.out.print("entrou aqui");
		        // seta os valores

		        stmt.setString(1,usuario.getNome());
		        stmt.setString(2,usuario.getLogin());
		        stmt.setString(3,usuario.getSenha());
		        stmt.setString(4,usuario.getRamo());
		        stmt.setString(5,usuario.getCnpj());
		        stmt.setInt(6,2);		        	      
		        stmt.setInt(7,Integer.parseInt(""+usuario.getIdPagina()));	  
		        stmt.setInt(8,Integer.parseInt(""+usuario.getQtd_cupons()));
		        stmt.setInt(9,Integer.parseInt(""+usuario.getPorcentagem_cupom()));
		   

		        // executa
		        stmt.execute();
		        System.out.print("add com sucesso");
		       // stmt.close();
		    } catch (SQLException e) {
		        throw new RuntimeException(e);
		    }
	    	}else{
	    	    System.out.print("Ja existe um usuario com esse login!");
	    	}
		}
	 
	 public int verificaUsuario(String login , String cnpj){
   	  
	    	
	    	try {	
				
				PreparedStatement stmt = (PreparedStatement) this.connection.prepareStatement("select idUsuarioComerciante from tb_usuario_comerciante where login='"+login+"' or cnpj='"+cnpj+"'");
				ResultSet rs= stmt.executeQuery(); 
				if(rs.next()){
	                	 
	                	 return 1;
	                	 
	                 }		
			    	 
				
					stmt.close();
					//connection.close();
				  
				 } catch (SQLException e) {
				         throw new RuntimeException(e);
				  }
	    
	    	
	    	return 0;
	    }
	    
	    public List<UsuarioComerciante> getLista() {
		try {	
		List<UsuarioComerciante> comerciantes = new ArrayList<UsuarioComerciante>();
		PreparedStatement stmt = (PreparedStatement) this.connection.prepareStatement("select * from tb_usuario_comerciante");
		ResultSet rs= stmt.executeQuery();
		
			while(rs.next()){
			    UsuarioComerciante comerciante = new UsuarioComerciante();
			    comerciante.setId(rs.getLong("idUsuarioComerciante"));
			    comerciante.setNome(rs.getString("nome"));
			    comerciante.setLogin(rs.getString("login"));
			    comerciante.setSenha(rs.getString("senha"));
			    comerciante.setRamo(rs.getString("ramo"));
			    comerciante.setCnpj(rs.getString("cpnj"));
			    comerciante.setAutenticacao(Integer.parseInt(rs.getString("autenticacao")));			 
			    comerciante.setIdPagina(rs.getLong("idPagina"));
			    
		
			      comerciantes.add(comerciante);
		        }
			rs.close();
			stmt.close();
			connection.close();
		     return comerciantes;
		 } catch (SQLException e) {
		         throw new RuntimeException(e);
		  }
	    }
	    
	    public boolean isLoginEqual(UsuarioComerciante usuario) {
			try {	
			List<UsuarioComerciante> clientes = new ArrayList<UsuarioComerciante>();
			PreparedStatement stmt = (PreparedStatement) this.connection.prepareStatement("select login from tb_usuario_comerciante where login='"+usuario.getLogin()+"' or cnpj='"+usuario.getCnpj()+"'");
			ResultSet rs= stmt.executeQuery();
		  // System.out.print(rs.getFetchSize());
				if(rs.next()){		
				   		  
			          return true;			
			      }
				rs.close();
				stmt.close();
				//connection.close();
			     return false;
			 } catch (SQLException e) {
			         throw new RuntimeException(e);
			  }
		    }
	    
	    
	    
	    public int validaAutenticacao(String login , String senha){
	    	
	    	PreparedStatement stmt;
			try {
				stmt = (PreparedStatement) this.connection.prepareStatement("select autenticacao from  tb_usuario_comerciante a  where a.login='"+login+"' and a.senha='"+senha+"'");
				ResultSet rs= stmt.executeQuery();
				
				while(rs.next()){
				   // UsuarioCliente cliente = new UsuarioCliente();
				    ///cliente.setId(rs.getLong("idUsuarioCliente"));
					
					
				      int autentica = Integer.parseInt(rs.getString("autenticacao"));				    
			          return autentica;
				     
			        }
				
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			
			
	    	
			return 0;
	    	
	    	
	    	
	    }
	
public UsuarioComerciante validaAutenticacaoUser(String login , String senha){
	    	
	    	PreparedStatement stmt;
			try {
				stmt = (PreparedStatement) this.connection.prepareStatement("select * from  tb_usuario_comerciante a  where a.login='"+login+"' and a.senha='"+senha+"'");
				ResultSet rs= stmt.executeQuery();
				
				while(rs.next()){
				     
					  UsuarioComerciante comerciante = new UsuarioComerciante();
					    comerciante.setId(rs.getLong("idUsuarioComerciante"));
					    comerciante.setNome(rs.getString("nome"));
					    comerciante.setLogin(rs.getString("login"));
					    comerciante.setSenha(rs.getString("senha"));
					    comerciante.setRamo(rs.getString("ramo"));
					    comerciante.setCnpj(rs.getString("cnpj"));
					    comerciante.setAutenticacao(Integer.parseInt(rs.getString("autenticacao")));
					 
					    comerciante.setIdPagina(rs.getLong("idPagina"));
					    
					    return comerciante;
				     
			        }
				
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			
			
	    	
			return null;
	    	
	    	
	    	
	    }
//buscar usuario via id
public UsuarioComerciante validaAutenticacaoUser(Long id){
	
	PreparedStatement stmt;
		try {
			stmt = (PreparedStatement) this.connection.prepareStatement("select * from  tb_usuario_comerciante a  where a.idUsuarioComerciante='"+id+"'");
			ResultSet rs= stmt.executeQuery();
			
			while(rs.next()){
			     
				  UsuarioComerciante comerciante = new UsuarioComerciante();
				    comerciante.setId(rs.getLong("idUsuarioComerciante"));
				    comerciante.setNome(rs.getString("nome"));
				    comerciante.setLogin(rs.getString("login"));
				    comerciante.setSenha(rs.getString("senha"));
				    comerciante.setRamo(rs.getString("ramo"));
				    comerciante.setCnpj(rs.getString("cnpj"));
				    comerciante.setAutenticacao(Integer.parseInt(rs.getString("autenticacao")));				  
				    comerciante.setIdPagina(rs.getLong("idPagina"));
				    
				    return comerciante;
			     
		        }
			
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		
	
		return null;
	
	
	
}


public UsuarioComerciante validaAutenticacaoUser(Long id , int autentica){
	
	PreparedStatement stmt;
		try {
			stmt = (PreparedStatement) this.connection.prepareStatement("select * from  tb_usuario_comerciante a  where a.idUsuarioComerciante='"+id+"' and autenticacao='"+autentica+"'");
			ResultSet rs= stmt.executeQuery();
			
			while(rs.next()){
			     
				  UsuarioComerciante comerciante = new UsuarioComerciante();
				    comerciante.setId(rs.getLong("idUsuarioComerciante"));
				    comerciante.setNome(rs.getString("nome"));
				    comerciante.setLogin(rs.getString("login"));
				    comerciante.setSenha(rs.getString("senha"));
				    comerciante.setRamo(rs.getString("ramo"));
				    comerciante.setCnpj(rs.getString("cnpj"));
				    comerciante.setAutenticacao(Integer.parseInt(rs.getString("autenticacao")));				  
				    comerciante.setIdPagina(rs.getLong("idPagina"));
				    
				    return comerciante;
			     
		        }
			stmt.close();
			rs.close();
			connection.close();
			
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}		
	
		return null;
	
	
	
}

public void updateComerciante(UsuarioComerciante usuario){
	
	   String sql = "UPDATE tb_usuario_comerciante SET nome=?, login=?, senha=?, ramo=?, cnpj=? WHERE idUsuarioComerciante=?";
	
	    try {
	        // prepared statement para inserção
	        PreparedStatement stmt = (PreparedStatement) connection.prepareStatement(sql);

	        // seta os valores


	        stmt.setString(1,usuario.getNome());
	        stmt.setString(2,usuario.getLogin());
	        stmt.setString(3,usuario.getSenha());
	        stmt.setString(4,usuario.getRamo());
	        stmt.setString(5,usuario.getCnpj());
	        stmt.setLong(6, usuario.getId());

	        // executa
	        stmt.executeUpdate();
	        System.out.print("Pagina add com sucesso");
	        stmt.close();
	    } catch (SQLException e) {
	        throw new RuntimeException(e);
	    }
	
}



}

